﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using PSLib.Parsing.Common;
using System.Globalization;

namespace PSLib.Parsing.Lua.ObjectModel
{
    public class ValueReference
    {
        #region Static members

        public static ValueReference Create(object value = null)
        {
            ValueReferenceType type = ValueReferenceType.Nil;
            if (value is string)
            {
                CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
                float fRes = 0;
                int iRes = 0;
                if (Int32.TryParse(value as string, out iRes)) value = iRes;
                else if (float.TryParse(value as string, NumberStyles.AllowDecimalPoint, culture, out fRes)) value = fRes;
                else
                {
                    type = ValueReferenceType.String;
                    value = Helper.TrimStringToVariableName(value as string);
                } 
            }

            if (value is bool) type = ValueReferenceType.Boolean;
            else if (value is Metatable) type = ValueReferenceType.Table;
            else if (value is Function) type = ValueReferenceType.Function;
            else if (value is int || value is float) type = ValueReferenceType.Number;

            return new ValueReference(type, type != ValueReferenceType.Nil ? value : null);
        }

        #endregion

        #region Constructor

        private ValueReference(ValueReferenceType type, object value = null)
        {
            this.Type = type;
            this.Value = value;
        }

        #endregion

        #region Properties

        public ValueReferenceType Type { get; private set; }
        public object Value { get; set; }

        #endregion
    }
}
